package com.pks.datacenter;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pks.datacenter.dto.MenuDto;
import com.pks.datacenter.entity.user.ModuleMenu;
import com.pks.datacenter.service.user.ModuleMenuService;
import com.pks.web.dto.ServiceResult;
import com.pks.web.springmvc.controller.BasicController;

@Controller
@RequestMapping("/index")
@Scope("prototype")
@Component("indexAction")
public class IndexAction extends BasicController {
	
	@Autowired
	private ModuleMenuService moduleMenuService;
	
	@RequestMapping("/index.do")
	public String index(HttpServletRequest request, Model model) {
		return "/WEB-INF/ftl/admin/index";
	}

	@RequestMapping("/top.do")
	public String top(Model model) {
		return "/WEB-INF/ftl/admin/top";
	}

	@RequestMapping("/container.do")
	public String container(Model model) {
		return "/WEB-INF/ftl/admin/container";
	}

	@RequestMapping("/left.do")
	public String left(Model model) {
		return "/WEB-INF/ftl/admin/left";
	}

	@RequestMapping("/start.do")
	public String start(HttpServletRequest request,Model model) {
		List<MenuDto> result = new ArrayList<MenuDto>();
		List<MenuDto> entryList = new ArrayList<MenuDto>();
//		User logined = WebContext.getLoginUser(request);
		Map<String, Object> qbuilder = new HashMap<String, Object>();
//		if(User.role_admin != logined.getRole()){
//			qbuilder.put("role", "%#"+logined.getRole()+"#%");
//		}
		qbuilder.put("index_state", 1);
		List<ModuleMenu> list = moduleMenuService.find("orders", qbuilder, null);
		List<MenuDto> r = new ArrayList<MenuDto>();
		String contextPath = request.getContextPath();
		for (ModuleMenu moduleMenu : list) {
			String url = null ;
			if(StringUtils.isNotBlank(moduleMenu.getPath())){
				url = contextPath + moduleMenu.getPath();
			}
			r.add(new MenuDto(moduleMenu.getId(), moduleMenu.getPid(), url, moduleMenu.getName(),moduleMenu.getIconfont()));
		}
		result.addAll(r);
		
		model.addAttribute("result", result);
		model.addAttribute("entryList", entryList);
		return "/WEB-INF/ftl/admin/start";
	}
	
	// 登录后的菜单列表
	@RequestMapping("/menu.ajax")
	public void menu(HttpServletRequest request, PrintWriter out) {
//		User loginUser = WebContext.getLoginUser(request);
		List<MenuDto> result = new ArrayList<MenuDto>();
		Map<String, Object> qbuilder = new HashMap<String, Object>();
//		if(User.role_admin != loginUser.getRole()){
//			qbuilder.put("role", "%#"+loginUser.getRole()+"#%");
//		}
		
		String url1 = request.getScheme()+"://"+ request.getServerName()+request.getRequestURI()+"?"+request.getQueryString();
		System.out.println("获取全路径(协议类型：//域名/项目名/命名空间/action名称?其他参数)url="+url1);
		String url2 = request.getScheme()+"://"+ request.getServerName();//+request.getRequestURI();
		System.out.println("协议名：//域名=" + url2);
		System.out.println("获取项目名=" + request.getContextPath());
		System.out.println("获取参数=" + request.getQueryString());
		System.out.println("获取全路径=" + request.getRequestURL());

		String contextPath = String.format("%s://%s%s", request.getScheme(),request.getServerName(),StringUtils.isBlank(request.getContextPath())?"":("/"+request.getContextPath()));
		contextPath = request.getContextPath();
		List<ModuleMenu> list = moduleMenuService.find("orders", qbuilder, null);
		for (ModuleMenu moduleMenu : list) {
			String url = null ;
			if(StringUtils.isNotBlank(moduleMenu.getPath())){
				url = contextPath + moduleMenu.getPath();
			}
			result.add(new MenuDto(moduleMenu.getId(), moduleMenu.getPid(), url, moduleMenu.getName()));
		}
		result = MenuDto.convertTree(result);
		List<MenuDto> l = new ArrayList<MenuDto>();
		for (MenuDto menuDto : result) {
			if(StringUtils.isNotBlank(menuDto.getUrl()) || (menuDto.getChilds()!=null && menuDto.getChilds().size()>0)){
				l.add(menuDto);
			}
		}
		if(result.size()>0){
			result.get(0).setOpen(true);
		}
		out.write(ServiceResult.initSuccessJson(l));
	}

	// 登录后的顶部子应用列表
	@RequestMapping("/menutop.ajax")
	public void menutop(HttpServletRequest request, PrintWriter out) {
		List<MenuDto> m3List = new ArrayList<MenuDto>();
		String result = ServiceResult.initSuccessJson(m3List);
		out.write(result);
	}
	
}
